home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / printing / std file saver / source / fileutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.5 KB  |  156 lines

  1. #include <Types.h>
  2. #include <Files.h>
  3. #include <Resources.h>
  4. #include <StandardFile.h>
  5. #include <Printing.h>
  6. #include <TextUtils.h>
  7.  
  8. #include "driverTypes.h"
  9.  
  10. OSErr SelectOutputFile(FSSpec *fileSpec)
  11. {
  12.     StandardFileReply    reply;
  13.     OSErr    theError;
  14.     StringHandle    prompt = GetString(defaultPromptID);
  15.     StringHandle    aStrHdl = GetString(defaultFileNameID);
  16.     
  17.     ///// The following typecasts are needed due to Radar ID#s
  18.     ///// 1341495, 1341498 and 1341516. SC is not following
  19.     ///// §A6.8 of The C Programming Language, Second Edition
  20.     ///// -DaveP 4/16/96
  21.     HLock((Handle)aStrHdl);
  22.     HLock((Handle)prompt);
  23.  
  24.     StandardPutFile(*prompt, *aStrHdl, &reply);
  25.  
  26.     HUnlock((Handle)aStrHdl);
  27.     ReleaseResource((Handle)aStrHdl);
  28.     HUnlock((Handle)prompt);
  29.     ReleaseResource((Handle)prompt);
  30.  
  31.     if (reply.sfGood) {
  32.         if (reply.sfReplacing) {
  33.             theError = FSpDelete(&(reply.sfFile));
  34.         } else {
  35.             theError = noErr;
  36.         }
  37.     } else theError = iPrAbort;
  38.     
  39.     *fileSpec = reply.sfFile;
  40.  
  41.     return theError;
  42. }
  43.  
  44.  
  45. OSErr OpenOutputFile(FSSpec *fileSpec, short *refnum, Boolean isResource)
  46. {
  47.     OSType    theType, theCreator;
  48.     OSErr theError;
  49.     
  50.     if (isResource) {
  51.         theType = 'rsrc';
  52.         theCreator = 'RSED';
  53.     } else {
  54.         theType = 'PICT';
  55.         theCreator = '????';
  56.     }
  57.  
  58.     theError = FSpCreate(fileSpec, theCreator, theType, smSystemScript);
  59.     if (theError != noErr) {
  60.         DebugStr("\pCreate failed");
  61.         return theError;
  62.     }
  63.  
  64.     if (isResource) {
  65.         short    saveResFile = CurResFile();
  66. //        DebugStr("\pOpening resource fork");
  67.         FSpCreateResFile(fileSpec, theCreator, theType, smSystemScript);
  68.         *refnum = FSpOpenResFile(fileSpec, fsRdWrPerm);
  69.         UseResFile(saveResFile);
  70.         if (*refnum == -1) theError = ResError();
  71.         else theError = noErr;
  72.     } else {
  73. //        DebugStr("\pOpening Data fork");
  74.         theError = FSpOpenDF(fileSpec, fsRdWrPerm, refnum);
  75.     }
  76. //    if (theError) DebugStr("\pOpen failed");
  77.  
  78.     return theError;
  79. }
  80.  
  81. OSErr CloseOutputFile(short refnum, Boolean isResource)
  82. {
  83.     OSErr theError;
  84.     
  85.     if (isResource) {
  86.         CloseResFile(refnum);
  87.         theError = ResError();
  88.     } else {
  89.         theError = FSClose(refnum);
  90.     }
  91.     return theError;
  92. }
  93.  
  94. OSErr WritePictData(short refnum, Handle pictHandle, short pageNum, Boolean isResource)
  95. {
  96.     OSErr    writeError;
  97.  
  98.     if (isResource) {
  99.         short    savedResFile = CurResFile();
  100.         
  101.         UseResFile(refnum);
  102.         
  103.         AddResource(pictHandle,'PICT',pageNum,(unsigned char *)"");
  104.         WriteResource(pictHandle);
  105.         ReleaseResource(pictHandle);
  106.         
  107.         UseResFile(savedResFile);
  108.     } else {
  109.         Ptr        gunkbytes;
  110.         long    theCount;
  111.  
  112.         gunkbytes = NewPtrClear(512);
  113.         if (!gunkbytes) {
  114.             DebugStr("\pCouldn't allocate garbage header bytes!");
  115.             return(iMemFullErr);
  116.         }
  117.  
  118.         theCount = 512;
  119.         writeError = FSWrite(refnum, &theCount, gunkbytes);
  120.         if (writeError != noErr) {
  121.             DebugStr("\pError writing file. Gack!");
  122.             return(writeError);
  123.         }
  124.         DisposePtr(gunkbytes);
  125.  
  126.         theCount = GetHandleSize(pictHandle);
  127.         writeError = FSWrite(refnum, &theCount, *pictHandle);
  128.         if (writeError != noErr) {
  129.             DebugStr("\pError writing file. Gack!");
  130.             return(writeError);
  131.         }
  132.         
  133.         DisposeHandle(pictHandle);
  134.     }
  135.     return(writeError);
  136. }
  137.  
  138. OSErr WriteCLUTData(short refnum)
  139. {
  140. #pragma unused(refnum)
  141. #ifdef SUPPORT_CLUTS
  142.     if (IsColorGrafPort((GrafPtr)pPrPort)) {
  143.         Handle    clutHandle;
  144.         Handle    tableHandle = (Handle) GetPmTable((CGrafPtr)pPrPort);
  145.         long    theCount = GetHandleSize(tableHandle);
  146.  
  147.         clutHandle = NewHandleClear(theCount);
  148.         if (clutHandle) {
  149.             BlockMoveData(*tableHandle, *clutHandle, theCount);
  150.             AddResource(clutHandle, 'clut', printRecord->CurPage, dummyString);
  151.             WriteResource(clutHandle);
  152.         }
  153.     }
  154. #endif
  155.     return noErr;
  156. }